package model

import common.Config
import common.Direction
import common.doDirectionAction
import model.abstractClass.AutoMoveView
import model.inter.Attackable
import model.inter.DestoryAble
import model.inter.Sufferable
import org.itheima.kotlin.game.core.Painter

/**
 * 创建一颗子弹，子弹是有方向和坐标的；
 * 子弹有自动移动的能力，
 * 子弹还有自动销毁的能力；
 *
 *
 * 这里有一个问题就是，我们怎么让子弹，只击打一次被击打者？
 */
class Bullet(override var mCurrentDirection: Direction,
             override val whoCreate: View,
             transformToXY: (width: Int, height: Int) -> Pair<Int, Int>
)
    : AutoMoveView(),
        DestoryAble,
        Attackable {
    override fun autoMove() {

        mCurrentDirection.doDirectionAction(
                {
                    y -= speed
                }, {
                    y += speed
                }, {
                    x -= speed
                }, {
                    x += speed
                }
        )
    }


    override var x: Int = 0;
    override var y: Int = 0;

    override var speed: Int = 16
    override var mIsWillHit: Boolean = false;

    var bulletImg: String = mCurrentDirection.doDirectionAction(
            { "img/shot_bottom.gif" },
            { "img/shot_top.gif" },
            { "img/shot_right.gif" },
            { "img/shot_left.gif" });

    var isHasAttack = false;


    init {

        mCurrentDirection.doDirectionAction(
                {
                    width = Config.BULLET_VERTICAL_DEFAULT_WIDTH
                    height = Config.BULLET_VERTICAL_DEFAULT_HEIGHT
                }, {
            width = Config.BULLET_VERTICAL_DEFAULT_WIDTH
            height = Config.BULLET_VERTICAL_DEFAULT_HEIGHT
        }, {

            width = Config.BULLET_HORIENTAL_DEFAULT_WIDTH
            height = Config.BULLET_HORIENTAL_DEFAULT_HEIGHT

        }, {

            width = Config.BULLET_HORIENTAL_DEFAULT_WIDTH
            height = Config.BULLET_HORIENTAL_DEFAULT_HEIGHT

        }
        )

        var pairs = transformToXY(width, height)

        x = pairs.first;
        y = pairs.second;

    }

    /**
     * 第一次攻击之后，将其攻击其置为0
     * 这里我们最好将被Attack的对象 返回回来，
     * 这样的话， 我们可以获取起对应的坐标；
     * 这样可以，有较好的显示效果；
     */
    override fun notifyAttactSuccess() {

        isHasAttack = true;
        println("攻击成功！！！！")
        //这里我们要放一个动画；
    }


    override var attactPower: Int = 1;


    override fun isNeedDestory(): Boolean {

        //是否已经移出到屏幕外边；

        var isMoveScreenOuter = mCurrentDirection.doDirectionAction(
                {
                    y <= -height
                }, {
            y >= height + Config.DEFAULT_WINDOW_HEIGHT
        }, {
            x <= -width;
        }, {
            x >= width + Config.DEFAULT_WINDOW_WIDTH
        }
        );

        println("Bullet:isNeedDestory:$isMoveScreenOuter")

        return isMoveScreenOuter || (isHasAttack)
    }


    override fun draw() {
        Painter.drawImage(bulletImg, x, y);
    }


    override fun attack(sufferable: Sufferable): Boolean {
        if((this.whoCreate is Enemy) and (sufferable is Enemy)){
            return false
        }

        if(whoCreate == sufferable){
            return false
        }

        return super.attack(sufferable)
    }
}